$(document).ready(function(){

		tree = new MyTree();
		tree.init()
		
		//$('#tabela').corner("round 8px").parent().css('padding', '5px').corner("round 10px");
		controles = new Controles();
		controles.init();
		FaixaEtaria.init();
		$("#gridhere").jqGrid(FaixaEtaria);
		

});

function MyTree(){
	this.init = function(){ 
		$("#menu-tree").jstree({
			//"core" : { "initially_open" : [ "root" ] },
	        "html_data" : {
	            "data" : $(".menu-tree").html()
	        },
	        "themes" : {
	        	            "theme" : "classic",
	        	            "dots" : true,
	        	            "icons" : true
	        	        },
	        "plugins" : [ "themes", "html_data" , "ui", "cookies" ],
	        cookie_options : {auto_save:true}
	        
		});	
		$("#menu-tree").delegate("a","click", function (e) { 
			document.location.href = this.href;
		});
		
		$("#menu-tree").bind("select_node.jstree", 
							function(e,data) 
							{
								//acrescentar eventos do onclick
        					}
		);
	};
}


FaixaEtaria ={
		datatype: "json",
	   	colNames:['codigo','descritivo','codigo_configuracao', 'minima', 'maxima','restricao_sexo'],
	   	colModel:[
	   		{name:'codigo', width:1},
	   		{name:'descritivo', width:180},
	   		{name:'codigo_configuracao', width:1, align:"center"},
	   		{name:'minima', width:80, align:"right"},		
	   		{name:'maxima', width:80,align:"right"},		
	   		{name:'restricao_sexo', width:1,align:"right"}
	   	],
		init:function (){
			codigo_configuracao = $("#codigo_configuracao").val();
			FaixaEtaria.url = '/indicadores/public/faixa-etaria/index/configuracao_id/'+codigo_configuracao;
		},
	   	rowNum:10,
	   	rowList:[10,20,30],
	   	pager: '#pager2',
	   	sortname: 'id',
	    viewrecords: true
	};




function showDescritivo(data,result,obj){
	if(data.toolbar != undefined){
		tabela = obj.attr('id');
		tabela = tabela.substr(3);
		$("#tb"+tabela).append("<tr><td>"+data.toolbar+"</td><td>"+data.obj.descritivo+"</td></tr>")
		obj[0].descritivo.value="";
		
	}else{
		/**
		 * enquanto ajusta todos os scripts de add
		 */
		if (data.obj.descritivo != undefined){
			tabela = obj.attr('id');
			tabela = tabela.substr(3);
			$("#tb"+tabela).append("<tr><td>"+data.obj.descritivo+"</td></tr>")
			obj[0].descritivo.value="";
			obj[0].minima.value="";
			obj[0].maxima.value="";
		}
	}
} 


function Controles(){
	this.init = function(){
		$('.fg-button').hover(
				function(){ 
					$(this).addClass("ui-state-hover"); 
				},
				function(){ 
					$(this).removeClass("ui-state-hover"); 
				}
			)
		$(function() {
			
			$("#formtabs").tabs({
				collapsible: true				
			});
			$(".make-tab").tabs({
				collapsible: true				
			});
		});
		
		$(
				function()
				{
					$(".byajax").click(function(e)
					{
						e.preventDefault()
					    var options = {
					        success:       showDescritivo,
					        dataType:	'json'
					    }; 
					    // bind form using 'ajaxForm'
					    formulario  = $(this).parents('form');
					    $(formulario).ajaxSubmit(options)				
					});
				}
		);


	    var icons = {
	            header: "ui-icon-circle-arrow-e",
	            headerSelected: "ui-icon-circle-arrow-s"
	      };
	      $(".accordion").accordion({
	          event: "mouseover",
	          icons:false,
	          autoHeight: false,
	          navigation: true
	      });        
		$(".draggable").draggable({
			placeholder: 'ui-state-highlight',
			cursor: 'move' ,
			tolerance: 'pointer',
			receive: function(event, ui) { 
						//alert('atualiza elemento no db') 
						} 
		});

		$(".draggable").disableSelection();		
	}		
}